package study.springcloud.discovery.consul.support.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Slf4j
//@Component
public class ServiceCloseUnRegisterListener {

    @Autowired(required = false)
    private ConsulServiceRegistry consulServiceRegistry;
    @Autowired
    private ConsulRegistration consulRegistration;

    @EventListener(ContextClosedEvent.class)
    public void doUnregister(ContextClosedEvent event) {
        log.info(">>>>>> {}", consulRegistration.getInstanceId());
        if (null != consulServiceRegistry) {
            consulServiceRegistry.deregister(consulRegistration);
        }
    }
}
